﻿Imports System.Reflection

<ComClass(ClsACadObjectEx.ClassId, ClsACadObjectEx.InterfaceId, ClsACadObjectEx.EventsId)>
Public Class ClsACadObjectEx

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Const ClassId As String = "9b9903d5-6dc0-4115-ae8d-e6605502b90c"
    Public Const InterfaceId As String = "a7bc556d-7f82-45a4-9ff7-0caa31101e10"
    Public Const EventsId As String = "7580653d-8fdf-42ce-a618-dcfbe4572419"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub

#Region "对CAD对象"

    Public Function LwPolyLineHasBulge(comPline As Object) As Boolean
        If TypeName(comPline) = $"IAcadLWPolyline" Then
            Dim ptsCount = (UBound(comPline.Coordinates) - LBound(comPline.Coordinates) - 1) / 2
            For index = 0 To ptsCount
                If Not Math.Asin(comPline.GetBulge(index)) < 0.00000001 Then
                    Return True
                End If
            Next
        End If
        Return False
    End Function


#End Region

#Region "xdata"
    Public Sub SetClassXdataToObject(ByRef acEnt As Object, xldatas As Object, Optional addInfors As Collection = Nothing, Optional splitor As String = "===>")
        Dim t As Type = xldatas.GetType()
        Dim setProps As List(Of PropertyInfo) = t.GetProperties.ToList()
        Dim DataTypes As New List(Of Short)
        Dim Datas As New List(Of Object)

        DataTypes.Add(1001) : Datas.Add(t.Name)
        For index = 1 To setProps.Count
            Dim p As PropertyInfo = setProps(index - 1)
            Dim val = p.Name & splitor & p.GetValue(xldatas, Nothing)
            DataTypes.Add(1000)
            Datas.Add(val)
        Next

        If addInfors IsNot Nothing Then
            For index = 1 To addInfors.Count
                DataTypes.Add(1000)
                Datas.Add(addInfors.Item(index))
            Next
        End If

        acEnt.SetXData(DataTypes.ToArray(), Datas.ToArray())
    End Sub

    Public Function GetXData(ByRef acEnt As Object, Optional xdataAppName As String = "") As String()
        Dim xdataOut As New Object
        Dim xtypeOut As New Object
        Dim resDic As New List(Of String)
        Call acEnt.GetXData(xdataAppName, xtypeOut, xdataOut)
        For i = LBound(xdataOut) To UBound(xdataOut)
            resDic.Add(xdataOut(i))
        Next
        Return resDic.ToArray()
    End Function

    Public Function IsEntityHasXData(acEnt As Object, Optional xdataAppName As String = "") As Boolean
        Dim xdataOut As New Object
        Dim xtypeOut As New Object
        Call acEnt.GetXData(xdataAppName, xtypeOut, xdataOut)
        If xdataOut Is Nothing Or xdataOut Is Nothing Then
            Return False
        Else
            Return True
        End If
    End Function

#End Region
End Class


